package bsi.vn.Lib;

import bsi.vn.Lib.Consts;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper{

	// Database creation sql statement
	private static final String BSI_COOKIE_CREATE = "create table "+ Consts.TABLE_COOKIE +" (" + Consts.DBF_ROWID + " integer primary key autoincrement, "
			+ Consts.DBF_USERID + " int not null, " + Consts.DBF_COOKIE + " text not null, " + Consts.DBF_IPADDRESS + " text not null, " + Consts.DBF_USERTYPE + " int not null);";
	private static final String BSI_LOCATION_CREATE = "create table "+ Consts.TABLE_LOCATION +" (" + Consts.DBF_ROWID + " integer primary key autoincrement, "
			+ Consts.DBF_LAT + " real not null, " + Consts.DBF_LNG + " real not null);";
	private static final String BSI_TICKET_CREATE = "create table "+ Consts.TABLE_TICKET +" (" + Consts.DBF_ROWID + " integer primary key autoincrement, "
			+ "Fcode text not null, Fmarket int not null, FBPrice real not null, Fvol int not null, FBDate int not null);";
	private static final String BSI_PROVINCE_CREATE = "create table "+ Consts.TABLE_PROVINCE +" (" + Consts.DBF_ROWID + " integer primary key, "
			+ Consts.DBF_PROVINCE +" text not null);";
	private static final String BSI_CUSTOMER_CREATE = "create table "+ Consts.TABLE_CUSTOMER +" (" + Consts.DBF_ROWID + " integer primary key, "
			+ Consts.DBF_CUSTOMER +" text not null);";
	private static final String BSI_ERRORLIST_CREATE = "create table "+ Consts.TABLE_ERRORLIST +" (" + Consts.DBF_ROWID + " integer primary key, "
			+ Consts.DBF_ERRORCODE +" text not null, "+ Consts.DBF_DESCRIPTION +" text not null);";
	private static final String BSI_ERRORDETAIL_CREATE = "create table "+ Consts.TABLE_ERRORDETAIL +" (" + Consts.DBF_ROWID + " integer primary key, "
			+ Consts.DBF_ERRORID +" int not null, " + Consts.DBF_ORDER + " int not null, " + Consts.DBF_DESCRIPTION +" text not null);";
	
	public DBHelper(Context context) {
		super(context, Consts.DATABASE_NAME, null, Consts.DATABASE_VERSION);
	}

	// Method is called during creation of the database
	@Override
	public void onCreate(SQLiteDatabase database) {
		database.execSQL("DROP TABLE IF EXISTS " + Consts.TABLE_COOKIE);
		database.execSQL("DROP TABLE IF EXISTS " + Consts.TABLE_LOCATION);
		database.execSQL("DROP TABLE IF EXISTS " + Consts.TABLE_TICKET);
		database.execSQL("DROP TABLE IF EXISTS " + Consts.TABLE_PROVINCE);
		database.execSQL("DROP TABLE IF EXISTS " + Consts.TABLE_CUSTOMER);
		database.execSQL("DROP TABLE IF EXISTS " + Consts.TABLE_ERRORLIST);
		database.execSQL("DROP TABLE IF EXISTS " + Consts.TABLE_ERRORDETAIL);
		database.execSQL(BSI_COOKIE_CREATE);
		database.execSQL(BSI_LOCATION_CREATE);
		database.execSQL(BSI_TICKET_CREATE);
		database.execSQL(BSI_PROVINCE_CREATE);
		database.execSQL(BSI_CUSTOMER_CREATE);
		database.execSQL(BSI_ERRORLIST_CREATE);
		database.execSQL(BSI_ERRORDETAIL_CREATE);
	}

	// Method is called during an upgrade of the database, e.g. if you increase
	// the database version
	@Override
	public void onUpgrade(SQLiteDatabase database, int oldVersion,
			int newVersion) {
		Log.w(DBHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
			onCreate(database);
	}
}